export function deepCopy(obj) {
    return JSON.parse(JSON.stringify(obj));
}

export function formatMoney(str) {
    str = str.toString();
    let num = str.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
    const num_array = num.split(".");
    const num_before = num_array[0].replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
    if(str.indexOf(".") == -1) {
        num_array[1] = '00';
    }else {
        if (num_array[1].length > 1) {
            num_array[1] = num_array[1].substr(0,2);
        }else if (num_array[1].length == 1) {
            num_array[1] = num_array[1] + '0';
        }else {
            num_array[1] = num_array[1] + '00';
        }
    }
    num = num_before+"."+num_array[1];
    return num;
}

export function getLeftDays(end) {
    const endTime = new Date(end).getTime();
    const now = new Date().getTime();
    if(endTime <= now) return 0;
    return Math.round((endTime - now) / (1000*60*60*24))
}

export function textLimit(text, max){
    if(!text) return "未知";
    return text.length > max ? text.substring(0, max) : text;
}

/**
 * 格式化时间
 * @param date
 * @param fmt 格式,默认格式yyyy-MM-dd HH:mm:ss
 * @returns {*}
 */
export function formatTime(date, fmt) {
    if (typeof date === 'string')date = parseInt(date);
    if(typeof date === "number")date = new Date(date);
    var o = {
        "M+": date.getMonth() + 1,                 //月份
        "d+": date.getDate(),                    //日
        "H+": date.getHours(),                   //小时
        "m+": date.getMinutes(),                 //分
        "s+": date.getSeconds(),                 //秒
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        "S": date.getMilliseconds()             //毫秒
    };
    if (!fmt) fmt = "yyyy-MM-dd HH:mm:ss";
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp(`(${k})`).test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

/**
 * 格式化时间到分钟
 * @param date
 * @returns {string}
 */
export function formatMinute(date) {
    return formatTime(date,'yyyy-MM-dd HH:mm')
}

/**
 * 格式化日期
 * @param date
 * @returns {*}
 */
export function formatDate(date) {
    return formatTime(date,'yyyy-MM-dd')
}

export function isIOS() {
    return /iPad|iPhone|iPod/.test(navigator.platform);
}

export function isApps() {
    return /MobileAppIafclub/.test(navigator.userAgent);
}

export function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    const regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
    const results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
